#include <iostream>
#include <vector>

using std::cout;
using std::endl;
using std::vector;

template <typename Container>
void display(const Container &con)
{
    for(auto &elem : con)
    {
        cout << elem << "  ";
    }
    cout << endl;
}

void printCapacity(const vector<int> &con)
{
    cout << "size() = " << con.size() << endl;
    cout << "capacity() = " << con.capacity() << endl;
}

void test()
{
    cout << "sizeof(vector<int>) = " << sizeof(vector<int>) << endl;
    cout << "sizeof(vector<long>) = " << sizeof(vector<long>) << endl;

    cout << endl;
    vector<int> number = {1, 3, 5, 7, 9, 8, 6, 4, 2, 10};
    display(number);
    printCapacity(number);

    cout << endl << "在vector的尾部进行插入与删除" << endl;
    number.push_back(100);
    number.push_back(200);
    display(number);
    printCapacity(number);
    number.pop_back();
    display(number);
    printCapacity(number);

    //Q:为何vector不能在头部进行插入与删除?
    //A:vector是一端开口的，如果提供在头部进行插入与删除的操作，
    //那么动第一个元素，会导致后面的N个元素都需要移动，时间
    //复杂度是O(N)

    //Q:获取vector的第一个元素的首地址
    &number;//error，不能得到第一个元素的首地址
    &number[0];//ok
    &*number.begin();//ok
    int *pdata = number.data();//ok

    cout << endl << "在vector的任意位置进行插入" << endl;
    /* vector<int>::iterator it = number.begin(); */
    auto it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 11);
    display(number);
    printCapacity(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, 5, 22);
    display(number);
    printCapacity(number);
    cout << "*it = " << *it << endl;

    //对于vector的insert操作而言，不知道每次插入元素的个数是
    //多少，当在进行插入的过程中，元素个数过多之后会进行扩容
    //，而迭代器还是指向的老的空间，然后再使用该迭代器的时候
    //会发生问题，因为迭代器已经失效了
    //
    //解决方案：每次在使用迭代器之前，将迭代器重新置位即可
    cout << endl << endl;
    vector<int> vec = {100, 300, 200, 500};
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, vec.begin(), vec.end());
    display(number);
    printCapacity(number);
    cout << "*it = " << *it << endl;

    cout << endl << endl;
    it = number.begin();
    ++it;
    ++it;
    cout << "*it = " << *it << endl;
    number.insert(it, {666, 999, 777, 555});
    display(number);
    printCapacity(number);
    cout << "*it = " << *it << endl;

    cout << endl << "清空元素" << endl;
    number.clear();//清空元素
    number.shrink_to_fit();//回收多余的空间
    cout << "size() = " << number.size() << endl;
    cout << "capacity() = " << number.capacity() << endl;
}

class Point
{
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int = 0, int = 0)" << endl;
    }

    Point(const Point &rhs)
    : _ix(rhs._ix)
    , _iy(rhs._iy)
    {
        cout << "Point(const Point &)" << endl;
    }

    void print()
    {
        cout << "void print()" << endl;
    }
private:
    int _ix;
    int _iy;
};

void test2()
{
    vector<Point> vec;
    Point pt(1, 2);
    /* cout << "======" << endl; */
    vec.push_back(pt);
    /* vec.push_back(Point(1, 2)); */
    /* vec.emplace_back(1, 2); */
    pt.print();
}

int main(int argc, char *argv[])
{
    test2();
    return 0;
}

